package com.ttk.issuetracker.model;

import java.util.List;
import java.util.Objects;

import javax.json.JsonObjectBuilder;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


/**
 * The persistent class for the company database table.
 * 
 */
@Entity
@Table(name="company")
@NamedQueries({
	@NamedQuery(name = "Company.findAll", query = "SELECT c FROM Company c ORDER BY c.companyName ASC"),
	@NamedQuery(name = "Company.findByIdCompany", query = "SELECT c FROM Company c WHERE c.idCompany = :idCompany"),
	@NamedQuery(name = "Company.findByCompanyName", query = "SELECT c FROM Company c WHERE c.companyName = :companyName")
})
public class Company extends AbstractEntity implements EntityItem<Integer> {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Basic(optional = false)
	@Column(name="id_company")
	private int idCompany;

	@Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 200)
	@Column(name="company_name")
	private String companyName;

	//bi-directional many-to-one association to Project
	@OneToMany(cascade = CascadeType.ALL, mappedBy="company")
	private List<Project> projects;

	public Company() {
	}

	public int getIdCompany() {
		return this.idCompany;
	}

	public void setIdCompany(int idCompany) {
		this.idCompany = idCompany;
	}

	public String getCompanyName() {
		return this.companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public List<Project> getProjects() {
		return this.projects;
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

	public Project addProject(Project project) {
		getProjects().add(project);
		project.setCompany(this);

		return project;
	}

	public Project removeProject(Project project) {
		getProjects().remove(project);
		project.setCompany(null);

		return project;
	}

	@Override
	public Integer getId() {
		return idCompany;
	}

	@Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Company other = (Company) obj;
        return Objects.equals(this.idCompany, other.idCompany);
    }

    @Override
    public String toString() {
        return "Company[ idCompany=" + idCompany + " ]";
    }

	@Override
	public void addJson(JsonObjectBuilder builder) {
		builder.add("idCompany", idCompany).add("companyName", companyName);
	}
}